package codetop.greedy;

public class Code134 {
    public int canCompleteCircuit(int[] gas, int[] cost) {
        int len=gas.length;
        int start=0;
        while (start<len){
            int step=0;
            int sum_cost=0;
            int sum_gas=0;
            while (step<len){
                int index=(start+step)%len;
                sum_gas+=gas[index];
                sum_cost+=cost[index];
                if (sum_gas<sum_cost){
                    break;
                }
                step++;
            }
            if (step==len)return start;
            else start=start+step+1;
        }
        return -1;
    }
}
